#include "chinactptrader.h"
#include <iostream>
#include <time.h>
#include <thread>
#include <QDebug>
#include <chrono>
#include <cstring>


// ---- 全局参数声明 ---- //
extern char gTradeFrontAddr[];                                // 模拟交易前置地址
time_t lOrderTime;
time_t lOrderOkTime;

ChinaCtpTrader::ChinaCtpTrader(
        CThostFtdcTraderApi *api,
        TThostFtdcBrokerIDType brokerID,
        TThostFtdcInvestorIDType investerID,     // 投资者账户名
        TThostFtdcPasswordType investerPassword, // 投资者密码
        TThostFtdcAppIDType appID,     // 投资者账户名
        TThostFtdcAuthCodeType authCode, // 投资者密码,
        QObject *parent)
    : QObject(parent), g_pTradeUserApi(api) {
    // 初始化登录信息（实际应用中从配置文件读取）
    /*
    gBrokerID[0] = '\0';
    gInvesterID[0] = '\0';
    gInvesterPassword[0] = '\0';
    gAppID[0] = '\0';
    gAuthCode[0] = '\0';
    */
    qstrncpy(gBrokerID, brokerID,5);  // 示例经纪商 ID
    qstrncpy(gInvesterID, investerID,12);  // 示例用户 ID
    qstrncpy(gInvesterPassword, investerPassword,32); // 示例密码
    qstrncpy(gAppID, appID,32); // 示例密码
    qstrncpy(gAuthCode, authCode,32); // 示例密码
}


void ChinaCtpTrader::OnFrontConnected()
{
    std::cout << "=====ChinaCtpTrader OnFrontConnected=====" << std::endl;
    // 开始登录
    reqAuthenticate();
}

void ChinaCtpTrader::reqAuthenticate()
{
    std::cout << "=====ChinaCtpTrader reqAuthenticate=====" << std::endl;
    CThostFtdcReqAuthenticateField authenticateReq{}; // 初始化所有成员为0（C++11及以上）

    // 安全复制BrokerID，检查长度避免溢出
    const size_t brokerIdMaxLen = sizeof(authenticateReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(authenticateReq.BrokerID, gBrokerID, brokerIdMaxLen);
    //authenticateReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止
    qDebug() << "gBrokerID" << authenticateReq.BrokerID;

    // 安全复制UserID
    const size_t userIdMaxLen = sizeof(authenticateReq.UserID) - 1;
    qstrncpy(authenticateReq.UserID, gInvesterID, userIdMaxLen);
    //authenticateReq.UserID[userIdMaxLen] = '\0';
    qDebug() << "gInvesterID" << authenticateReq.UserID;

    // 安全复制Password
    const size_t appIDMaxLen = sizeof(authenticateReq.AppID) - 1;
    qstrncpy(authenticateReq.AppID, gAppID, appIDMaxLen);
    //authenticateReq.AppID[appIDMaxLen] = '\0';
    qDebug() << "appID：" << authenticateReq.AppID;

    // 安全复制Password
    const size_t authCodeMaxLen = sizeof(authenticateReq.AuthCode) - 1;
    qstrncpy(authenticateReq.AuthCode, gAuthCode, authCodeMaxLen);
   // authenticateReq.AuthCode[authCodeMaxLen] = '\0';
    qDebug() << "gAuthCode" << gAuthCode;

    static int requestID = 0; // 请求编号
    int rt = g_pTradeUserApi->ReqAuthenticate(&authenticateReq, requestID);
    if (!rt)
        std::cout << ">>>>>>reqAuthenticate success" << std::endl;
    else
        std::cerr << "--->>>reqAuthenticate failed" << std::endl;
}

void ChinaCtpTrader::OnRspAuthenticate(
    CThostFtdcRspAuthenticateField *pRspAuthenticateField,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    std::cout << "=====ChinaCtpTrader OnRspAuthenticate=====" << std::endl;
    if (!isErrorRspInfo(pRspInfo))
    {
        reqUserLogin();
    }
}

void ChinaCtpTrader::OnRspUserLogin(
    CThostFtdcRspUserLoginField *pRspUserLogin,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        std::cout << "=====账户登录成功=====" << std::endl;
        loginFlag = true;
        std::cout << "交易日： " << pRspUserLogin->TradingDay << std::endl;
        std::cout << "登录时间： " << pRspUserLogin->LoginTime << std::endl;
        std::cout << "经纪商： " << pRspUserLogin->BrokerID << std::endl;
        std::cout << "帐户名： " << pRspUserLogin->UserID << std::endl;
        // 保存会话参数
        trade_front_id = pRspUserLogin->FrontID;
        session_id = pRspUserLogin->SessionID;

        // 投资者结算结果确认
        reqSettlementInfoConfirm();
    }
}

void ChinaCtpTrader::OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast)
{
    std::cerr << "=====OnRspError=====" << std::endl;
    isErrorRspInfo(pRspInfo);
}

void ChinaCtpTrader::OnFrontDisconnected(int nReason)
{
    std::cerr << "=====OnFrontDisconnected=====" << std::endl;
    std::cerr << "错误码： " << nReason << std::endl;
}

void ChinaCtpTrader::OnHeartBeatWarning(int nTimeLapse)
{
    std::cerr << "=====网络心跳超时=====" << std::endl;
    std::cerr << "距上次连接时间： " << nTimeLapse << std::endl;
}

void ChinaCtpTrader::OnRspUserLogout(
    CThostFtdcUserLogoutField *pUserLogout,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        loginFlag = false; // 登出就不能再交易了
        std::cout << "=====账户登出成功=====" << std::endl;
        std::cout << "经纪商： " << pUserLogout->BrokerID << std::endl;
        std::cout << "帐户名： " << pUserLogout->UserID << std::endl;
    }
}

void ChinaCtpTrader::OnRspSettlementInfoConfirm(
    CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        std::cout << "=====投资者结算结果确认成功=====" << std::endl;
        std::cout << "确认日期： " << pSettlementInfoConfirm->ConfirmDate << std::endl;
        std::cout << "确认时间： " << pSettlementInfoConfirm->ConfirmTime << std::endl;
        // 请求查询合约
        reqQueryInstrument();
    }
}

void ChinaCtpTrader::OnRspQryInstrument(
    CThostFtdcInstrumentField *pInstrument,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        std::cout << "=====查询合约结果成功=====" << std::endl;
        std::cout << "交易所代码： " << pInstrument->ExchangeID << std::endl;
        std::cout << "合约代码： " << pInstrument->InstrumentID << std::endl;
        std::cout << "合约在交易所的代码： " << pInstrument->ExchangeInstID << std::endl;
        std::cout << "执行价： " << pInstrument->StrikePrice << std::endl;
        std::cout << "到期日： " << pInstrument->EndDelivDate << std::endl;
        std::cout << "当前交易状态： " << pInstrument->IsTrading << std::endl;
        // 请求查询投资者资金账户
        //reqQueryTradingAccount();
    }
}

void ChinaCtpTrader::OnRspQryTradingAccount(
    CThostFtdcTradingAccountField *pTradingAccount,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        std::cout << "=====查询投资者资金账户成功=====" << std::endl;
        std::cout << "投资者账号： " << pTradingAccount->AccountID << std::endl;
        std::cout << "可用资金： " << pTradingAccount->Available << std::endl;
        std::cout << "可取资金： " << pTradingAccount->WithdrawQuota << std::endl;
        std::cout << "当前保证金: " << pTradingAccount->CurrMargin << std::endl;
        std::cout << "平仓盈亏： " << pTradingAccount->CloseProfit << std::endl;
        // 请求查询投资者持仓
        //reqQueryInvestorPosition();
    }
}

void ChinaCtpTrader::OnRspQryInvestorPosition(
    CThostFtdcInvestorPositionField *pInvestorPosition,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        std::cout << "=====查询投资者持仓成功=====" << std::endl;
        if (pInvestorPosition)
        {
            std::cout << "合约代码： " << pInvestorPosition->InstrumentID << std::endl;
            std::cout << "开仓价格： " << pInvestorPosition->OpenAmount << std::endl;
            std::cout << "开仓量： " << pInvestorPosition->OpenVolume << std::endl;
            std::cout << "开仓方向： " << pInvestorPosition->PosiDirection << std::endl;
            std::cout << "占用保证金：" << pInvestorPosition->UseMargin << std::endl;
        }
        else
            std::cout << "----->该合约未持仓" << std::endl;
    }
}

void ChinaCtpTrader::OnRspOrderInsert(
    CThostFtdcInputOrderField *pInputOrder,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        std::cout << "=====报单录入成功=====" << std::endl;
        std::cout << "合约代码： " << pInputOrder->InstrumentID << std::endl;
        std::cout << "价格： " << pInputOrder->LimitPrice << std::endl;
        std::cout << "数量： " << pInputOrder->VolumeTotalOriginal << std::endl;
        std::cout << "开仓方向： " << pInputOrder->Direction << std::endl;
    }
}

void ChinaCtpTrader::OnRspOrderAction(
    CThostFtdcInputOrderActionField *pInputOrderAction,
    CThostFtdcRspInfoField *pRspInfo,
    int nRequestID,
    bool bIsLast)
{
    if (!isErrorRspInfo(pRspInfo))
    {
        std::cout << "=====报单操作成功=====" << std::endl;
        std::cout << "合约代码： " << pInputOrderAction->InstrumentID << std::endl;
        std::cout << "操作标志： " << pInputOrderAction->ActionFlag;
    }
}

void ChinaCtpTrader::OnRtnOrder(CThostFtdcOrderField *pOrder)
{
    char str[10];
    sprintf(str, "%d", pOrder->OrderSubmitStatus);
    int orderState = atoi(str) - 48;	//报单状态0=已经提交，3=已经接受

    std::cout << "=====收到报单应答=====" << std::endl;

    if (isMyOrder(pOrder))
    {
        if (isTradingOrder(pOrder))
        {
            std::cout << "--->>> 等待成交中！" << std::endl;
            //reqOrderAction(pOrder); // 这里可以撤单
            //reqUserLogout(); // 登出测试
        }
        else if (pOrder->OrderStatus == THOST_FTDC_OST_Canceled)
            std::cout << "--->>> 撤单成功！" << std::endl;
    }
}

void ChinaCtpTrader::OnRtnTrade(CThostFtdcTradeField *pTrade)
{
    std::cout << "=====报单成功成交=====" << std::endl;
    std::cout << "成交时间： " << pTrade->TradeTime << std::endl;
    std::cout << "合约代码： " << pTrade->InstrumentID << std::endl;
    std::cout << "成交价格： " << pTrade->Price << std::endl;
    std::cout << "成交量： " << pTrade->Volume << std::endl;
    std::cout << "开平仓方向： " << pTrade->Direction << std::endl;
}

bool ChinaCtpTrader::isErrorRspInfo(CThostFtdcRspInfoField *pRspInfo)
{
    bool bResult = pRspInfo && (pRspInfo->ErrorID != 0);
    if (bResult)
        std::cerr << "isErrorRspInfo--->>> ErrorID=" << pRspInfo->ErrorID << ", ErrorMsg=" << pRspInfo->ErrorMsg << std::endl;
    return bResult;
}

void ChinaCtpTrader::reqUserLogin()
{
    CThostFtdcReqUserLoginField loginReq{}; // 初始化所有成员为0（C++11及以上）

    // 安全复制BrokerID，检查长度避免溢出
    const size_t brokerIdMaxLen = sizeof(loginReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(loginReq.BrokerID, gBrokerID, brokerIdMaxLen);
    loginReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    // 安全复制UserID
    const size_t userIdMaxLen = sizeof(loginReq.UserID) - 1;
    qstrncpy(loginReq.UserID, gInvesterID, userIdMaxLen);
    loginReq.UserID[userIdMaxLen] = '\0';

    // 安全复制Password
    const size_t passwordMaxLen = sizeof(loginReq.Password) - 1;
    qstrncpy(loginReq.Password, gInvesterPassword, passwordMaxLen);
    loginReq.Password[passwordMaxLen] = '\0';

    static int requestID = 0; // 请求编号
    int rt = g_pTradeUserApi->ReqUserLogin(&loginReq, requestID);
    if (!rt)
        std::cout << ">>>>>>reqUserLogin success" << std::endl;
    else
        std::cerr << "--->>>reqUserLogin failed" << std::endl;
}

void ChinaCtpTrader::reqUserLogout()
{
    CThostFtdcUserLogoutField logoutReq{};

    // 安全复制BrokerID，检查长度避免溢出
    const size_t brokerIdMaxLen = sizeof(logoutReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(logoutReq.BrokerID, gBrokerID, brokerIdMaxLen);
    logoutReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    // 安全复制UserID
    const size_t userIdMaxLen = sizeof(logoutReq.UserID) - 1;
    qstrncpy(logoutReq.UserID, gInvesterID, userIdMaxLen);
    logoutReq.UserID[userIdMaxLen] = '\0';

    static int requestID = 0; // 请求编号
    int rt = g_pTradeUserApi->ReqUserLogout(&logoutReq, requestID);
    if (!rt)
        std::cout << ">>>>>>发送登出请求成功" << std::endl;
    else
        std::cerr << "--->>>发送登出请求失败" << std::endl;
}


void ChinaCtpTrader::reqSettlementInfoConfirm()
{
    CThostFtdcSettlementInfoConfirmField settlementConfirmReq{};

    // 安全复制BrokerID，检查长度避免溢出
    const size_t brokerIdMaxLen = sizeof(settlementConfirmReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(settlementConfirmReq.BrokerID, gBrokerID, brokerIdMaxLen);
    settlementConfirmReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    // 安全复制UserID
    const size_t userIdMaxLen = sizeof(settlementConfirmReq.InvestorID) - 1;
    qstrncpy(settlementConfirmReq.InvestorID, gInvesterID, userIdMaxLen);
    settlementConfirmReq.InvestorID[userIdMaxLen] = '\0';


    static int requestID = 0; // 请求编号
    int rt = g_pTradeUserApi->ReqSettlementInfoConfirm(&settlementConfirmReq, requestID);
    if (!rt)
        std::cout << ">>>>>>发送投资者结算结果确认请求成功" << std::endl;
    else
        std::cerr << "--->>>发送投资者结算结果确认请求失败" << std::endl;
}

void ChinaCtpTrader::reqQueryInstrument()
{
    CThostFtdcQryInstrumentField instrumentReq{};
    // 安全复制BrokerID，检查长度避免溢出
    const size_t brokerIdMaxLen = sizeof(instrumentReq.InstrumentID) - 1; // 预留终止符位置
    qstrncpy(instrumentReq.InstrumentID, gBrokerID, brokerIdMaxLen);
    instrumentReq.InstrumentID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    static int requestID = 0; // 请求编号
    int rt = g_pTradeUserApi->ReqQryInstrument(&instrumentReq, requestID);
    if (!rt)
        std::cout << ">>>>>>发送合约查询请求成功" << std::endl;
    else
        std::cerr << "--->>>发送合约查询请求失败" << std::endl;
}

void ChinaCtpTrader::reqQueryTradingAccount()
{
    CThostFtdcQryTradingAccountField tradingAccountReq{};

    // 安全复制BrokerID，检查长度避免溢出
    const size_t brokerIdMaxLen = sizeof(tradingAccountReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(tradingAccountReq.BrokerID, gBrokerID, brokerIdMaxLen);
    tradingAccountReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    // 安全复制UserID
    const size_t userIdMaxLen = sizeof(tradingAccountReq.InvestorID) - 1;
    qstrncpy(tradingAccountReq.InvestorID, gInvesterID, userIdMaxLen);
    tradingAccountReq.InvestorID[userIdMaxLen] = '\0';

    static int requestID = 0; // 请求编号
    std::this_thread::sleep_for(std::chrono::milliseconds(700)); // 有时候需要停顿一会才能查询成功
    int rt = g_pTradeUserApi->ReqQryTradingAccount(&tradingAccountReq, requestID);
    if (!rt)
        std::cout << ">>>>>>发送投资者资金账户查询请求成功" << std::endl;
    else
        std::cerr << "--->>>发送投资者资金账户查询请求失败" << std::endl;
}

void ChinaCtpTrader::reqQueryInvestorPosition()
{
    CThostFtdcQryInvestorPositionField postionReq{};

    // 安全复制BrokerID，检查长度避免溢出
    const size_t brokerIdMaxLen = sizeof(postionReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(postionReq.BrokerID, gBrokerID, brokerIdMaxLen);
    postionReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    // 安全复制UserID
    const size_t userIdMaxLen = sizeof(postionReq.InvestorID) - 1;
    qstrncpy(postionReq.InvestorID, gInvesterID, userIdMaxLen);
    postionReq.InvestorID[userIdMaxLen] = '\0';

    // 安全复制UserID
    //const size_t instrumentIDMaxLen = sizeof(postionReq.InstrumentID) - 1;
    //qstrncpy(postionReq.InstrumentID, g_pTradeInstrumentID, userIdMaxLen);
    //postionReq.InstrumentID[instrumentIDMaxLen] = '\0';

    static int requestID = 0; // 请求编号
    std::this_thread::sleep_for(std::chrono::milliseconds(700)); // 有时候需要停顿一会才能查询成功
    int rt = g_pTradeUserApi->ReqQryInvestorPosition(&postionReq, requestID);
    if (!rt)
        std::cout << ">>>>>>发送投资者持仓查询请求成功" << std::endl;
    else
        std::cerr << "--->>>发送投资者持仓查询请求失败" << std::endl;
}

void ChinaCtpTrader::reqOrderInsert(
    TThostFtdcOrderRefType orderRef,
    TThostFtdcExchangeIDType exchangeID,
    TThostFtdcInstrumentIDType instrumentID,
    TThostFtdcPriceType price,
    TThostFtdcVolumeType volume,
    bool isCloseOrder,bool isBuyOrder,bool isTodayOrder)
{
    CThostFtdcInputOrderField orderInsertReq{};
    ///经纪公司代码

    const size_t brokerIdMaxLen = sizeof(orderInsertReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(orderInsertReq.BrokerID, gBrokerID, brokerIdMaxLen);
    orderInsertReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    const size_t inventorIdMaxLen = sizeof(orderInsertReq.InvestorID) - 1; // 预留终止符位置
    qstrncpy(orderInsertReq.InvestorID, gInvesterID, inventorIdMaxLen);
    orderInsertReq.InvestorID[inventorIdMaxLen] = '\0'; // 确保字符串终止


    ///合约代码
    const size_t instrumentIdMaxLen = sizeof(orderInsertReq.InvestorID) - 1; // 预留终止符位置
    qstrncpy(orderInsertReq.InstrumentID, instrumentID, instrumentIdMaxLen);
    orderInsertReq.InstrumentID[instrumentIdMaxLen] = '\0'; // 确保字符串终止


    ///报单引用
    const size_t orderRefMaxLen = sizeof(orderInsertReq.OrderRef) - 1; // 预留终止符位置
    qstrncpy(orderInsertReq.OrderRef, orderRef, orderRefMaxLen);
    orderInsertReq.OrderRef[orderRefMaxLen] = '\0'; // 确保字符串终止

    const size_t exchangeIDIdMaxLen = sizeof(orderInsertReq.ExchangeID) - 1; // 预留终止符位置
    qstrncpy(orderInsertReq.ExchangeID, exchangeID, exchangeIDIdMaxLen);
    orderInsertReq.ExchangeID[exchangeIDIdMaxLen] = '\0'; // 确保字符串终止

    ///报单价格条件: 限价
    orderInsertReq.OrderPriceType = THOST_FTDC_OPT_LimitPrice;
    ///买卖方向:
    if(isBuyOrder){
        orderInsertReq.Direction = THOST_FTDC_D_Buy;
    }else{
        orderInsertReq.Direction = THOST_FTDC_D_Sell;
    }
    ///组合开平标志: 开仓
    orderInsertReq.CombOffsetFlag[0] = THOST_FTDC_OF_Open;
    if(isCloseOrder){
        ///组合开平标志: 平仓
        if(isTodayOrder){
            orderInsertReq.CombOffsetFlag[0] = THOST_FTDC_OF_Close;
            ///组合开平标志: 平今仓
            orderInsertReq.CombOffsetFlag[0] = THOST_FTDC_OF_CloseToday;
        }else{

            ///组合开平标志: 平昨仓
            orderInsertReq.CombOffsetFlag[0] = THOST_FTDC_OF_CloseYesterday;
        }
    }
    ///组合投机套保标志
    orderInsertReq.CombHedgeFlag[0] = THOST_FTDC_HF_Speculation;
    ///价格
    orderInsertReq.LimitPrice = price;
    ///数量：1
    orderInsertReq.VolumeTotalOriginal = volume;
    ///有效期类型: 当日有效
    orderInsertReq.TimeCondition = THOST_FTDC_TC_GFD;
    ///成交量类型: 任何数量
    orderInsertReq.VolumeCondition = THOST_FTDC_VC_AV;
    ///最小成交量: 1
    orderInsertReq.MinVolume = 1;
    ///触发条件: 立即
    orderInsertReq.ContingentCondition = THOST_FTDC_CC_Immediately;
    ///强平原因: 非强平
    orderInsertReq.ForceCloseReason = THOST_FTDC_FCC_NotForceClose;
    ///自动挂起标志: 否
    orderInsertReq.IsAutoSuspend = 0;
    ///用户强评标志: 否
    orderInsertReq.UserForceClose = 0;

    static int requestID = 0; // 请求编号
    int rt = g_pTradeUserApi->ReqOrderInsert(&orderInsertReq, ++requestID);
    if (!rt)
        std::cout << ">>>>>>发送报单录入请求成功" << std::endl;
    else
        std::cerr << "--->>>发送报单录入请求失败" << std::endl;
}

void ChinaCtpTrader::reqOrderAction(TThostFtdcInstrumentIDType instrumentID,const QString revokeOrderID)
{
    static bool orderActionSentFlag = false; // 是否发送了报单
    if (orderActionSentFlag)
        return;

    CThostFtdcInputOrderActionField orderActionReq{};

    const size_t brokerIdMaxLen = sizeof(orderActionReq.BrokerID) - 1; // 预留终止符位置
    qstrncpy(orderActionReq.BrokerID, gBrokerID, brokerIdMaxLen);
    orderActionReq.BrokerID[brokerIdMaxLen] = '\0'; // 确保字符串终止

    const size_t inventorIdMaxLen = sizeof(orderActionReq.InvestorID) - 1; // 预留终止符位置
    qstrncpy(orderActionReq.InvestorID, gInvesterID, inventorIdMaxLen);
    orderActionReq.InvestorID[inventorIdMaxLen] = '\0'; // 确保字符串终止


    ///合约代码
    const size_t instrumentIdMaxLen = sizeof(orderActionReq.InstrumentID) - 1; // 预留终止符位置
    qstrncpy(orderActionReq.InstrumentID, instrumentID, instrumentIdMaxLen);
    orderActionReq.InstrumentID[instrumentIdMaxLen] = '\0'; // 确保字符串终止


    ///报单引用
    const size_t orderRefMaxLen = sizeof(orderActionReq.OrderRef) - 1; // 预留终止符位置
    qstrncpy(orderActionReq.OrderRef, revokeOrderID.toUtf8().data(), orderRefMaxLen);
    orderActionReq.OrderRef[orderRefMaxLen] = '\0'; // 确保字符串终止

    ///请求编号
    //	TThostFtdcRequestIDType	RequestID;
    ///前置编号
    orderActionReq.FrontID = trade_front_id;
    ///会话编号
    orderActionReq.SessionID = session_id;
    ///交易所代码
    //	TThostFtdcExchangeIDType	ExchangeID;
    ///报单编号
    //	TThostFtdcOrderSysIDType	OrderSysID;
    ///操作标志
    orderActionReq.ActionFlag = THOST_FTDC_AF_Delete;
    ///价格
    //	TThostFtdcPriceType	LimitPrice;
    ///数量变化
    //	TThostFtdcVolumeType	VolumeChange;
    ///用户代码
    //	TThostFtdcUserIDType	UserID;
    static int requestID = 0; // 请求编号
    int rt = g_pTradeUserApi->ReqOrderAction(&orderActionReq, ++requestID);
    if (!rt)
        std::cout << ">>>>>>发送报单操作请求成功" << std::endl;
    else
        std::cerr << "--->>>发送报单操作请求失败" << std::endl;
    orderActionSentFlag = true;
}

bool ChinaCtpTrader::isMyOrder(CThostFtdcOrderField *pOrder)
{
    return ((pOrder->FrontID == trade_front_id) &&
            (pOrder->SessionID == session_id));
}

bool ChinaCtpTrader::isTradingOrder(CThostFtdcOrderField *pOrder)
{
    return ((pOrder->OrderStatus != THOST_FTDC_OST_PartTradedNotQueueing) &&
            (pOrder->OrderStatus != THOST_FTDC_OST_Canceled) &&
            (pOrder->OrderStatus != THOST_FTDC_OST_AllTraded));
}
